<template>
	<view class="tabbarpage-container welcome-container">
		<div class="logo-container">
			<img src="@/static/logo.png" mode="aspectFit" />
		</div>
		<div class="h1">
			AI英语错题侦探
		</div>
		<div class="h2">
			- 您的智能英语学习助手 -
		</div>
	</view>
</template>

<script setup>
import { onBeforeMount } from 'vue';
import { globalProps } from '@/js/global.js'
	
	onBeforeMount(()=>{
		//获取胶囊参数
		getStatusBarHeight()
		//登录
		getOpenId()
		//模拟登陆成功
		// globalProps.userInfo = {
		// 	"id":33,
		// 	"nickname":"张平文",
		// 	"openid":"oSPvF63G9ALEILFJ19SP6oLu4Gjg",
		// 	"province":null,
		// 	"city":null,
		// 	"country":null,
		// 	"sex":0,
		// 	"headimgurl":null,
		// 	"selflevel":0,
		// 	"overlevel":0,
		// 	"description":null
		// }
		// pageJump()
	})
	
	//登录
	const getOpenId = ()=>{
		const baseApi = globalProps.baseApi
		uni.login({
			success: (res) => {
				if(res.code){
					uni.request({
						method:'GET',
						url:baseApi+'/authenticate/login',
						data:{
							code: res.code
						},
						//登陆成功
						success:(res) => {
							console.log('登陆后获取userInfo'+JSON.stringify(res.data))
							if(res.statusCode === 502){
								uni.showToast({
								  title: '服务器未开启!',
								  icon: 'error',
								  duration: 1000000,
								  mask: true
								});
								return
							}
							if(res.data){
								pageJump()
								globalProps.userInfo = res.data
								
							}else{
								//登陆失败
								console.log(res)
								uni.showToast({
								  title: '服务器响应失败!',
								  icon: 'error',
								  duration: 1000000,
								  mask: true
								});
							}
						},
						//登陆失败
						fail: (err) => {
							console.log(err)
							uni.showToast({
							  title: '服务器响应失败!',
							  icon: 'error',
							  duration: 1000000,
							  mask: true
							});
						}
					})
				}else{
					uni.showToast({
						title:'登陆失败，请检查网络',
						icon:'error',
						duration: 1000000,
						mask: true
					})
				}
			}
		})
	}
	//登陆成功，页面跳转
	const pageJump = ()=>{
		setTimeout(()=>{
			uni.switchTab({
				url: '/pages/HomePage/HomePage'
			})
		},1000)
	}
	const getStatusBarHeight = ()=>{
		// #ifdef MP-WEIXIN
		getStatusBarHeightWx()
		// #endif
		// #ifdef APP-PLUS
		getStatusBarHeightApp()
		// #endif
	}
	// #ifdef MP-WEIXIN
	//获取顶部padding
	const getStatusBarHeightWx = ()=>{
		const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
		globalProps.capsuleHeight = menuButtonInfo.height + 'px';
		globalProps.statusBarHeight = menuButtonInfo.top + 'px';
	}
	// #endif
	// #ifdef APP-PLUS
	//获取顶部padding
	const getStatusBarHeightApp = ()=>{
		uni.getSystemInfo({
			success: (res) => {
				globalProps.capsuleHeight = '32px';
				globalProps.statusBarHeight = res.statusBarHeight + 'px';
			}
		});
	}
	// #endif
</script>

<style>
	@import "@/css/global.css";
	@import "@/css/animation.css";
	.welcome-container{
		align-items: center;
		justify-content: center;
	}
	.logo-container{
		width: 80px;
		height: 80px;
		display: flex;
		align-items: center;
		justify-content: center;
		animation: top-in-ani .7s 1;
	}
	.h1{
		color: #fff;
		font-weight: bold;
		font-size: 23px;
		letter-spacing: 2px;
		margin: 4px 0 12px 0;
		animation: letter-spaceing-ani 1 .8s;
	}
	.h2{
		color: #fff;
		font-size: 14px;
		letter-spacing: 1px;
		margin-bottom: 8px;
		animation: letter-spaceing-ani 1 1s;
	}
</style>
